package com.web44.springboot.config;

import com.web44.springboot.Interceptor.ErrorInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.concurrent.TimeUnit;

/**
 * @Title: SpringMvcConfig
 * @Description:
 * @Auther: py
 * @Version: 1.0
 * @create 2024/2/29 14:36
 */
@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {

    @Autowired
    private ErrorInterceptor errorInterceptor;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        registry.addViewController("/home.html").setViewName("home");

    }

    /**
     * addResourceHandler:sping.mvc.static-path-pattern 请求路径
     * addResourceLocations:spring.web.resources.static-locations  本地静态资源的位置
     * @param registry
     */
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/**")
//                .addResourceLocations("classpath:/static/","classpath:/public/")
//                // 设置缓存
//                .setCacheControl(CacheControl.maxAge(3600, TimeUnit.SECONDS));
//    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(errorInterceptor).addPathPatterns("/*");
    }
}
